package com.briup.service.impl;

import com.briup.bean.ShopCar;
import com.briup.dao.ShopCarMapper;
import com.briup.service.IShopCarService;
import com.briup.util.MyBatisSqlSessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author 王若龙
 * @CreateTime 2022-09-26 11:41
 */
public class IShopCarServiceImpl implements IShopCarService {
     SqlSession sqlSession = MyBatisSqlSessionFactory.openSession(true);
    private ShopCarMapper shopCarMapper = sqlSession.getMapper(ShopCarMapper.class);

    @Override
    public void saveShopCar(ShopCar sc) {
        ShopCar shopCar = queryShopCarByCidAndBId(sc.getCustomerId(), sc.getBookId());
        if (shopCar == null){
            shopCarMapper.saveShopCar(sc);
        }else {
            int num = shopCar.getNum() + sc.getNum();
            sc.setNum(num);
            updateShopCar(sc);
        }
    }

    @Override
    public void updateShopCar(ShopCar sc) {
        shopCarMapper.updateShopCar(sc);
    }

    @Override
    public ShopCar queryShopCarByCidAndBId(int cid, int bid) {
        return shopCarMapper.queryShopCarByCidAndBId(cid, bid);
    }

    @Override
    public List<ShopCar> findShopCarsByCustomer(Integer id) {
        sqlSession.clearCache();
        return shopCarMapper.findShopCarsByCustomer(id);
    }

    @Override
    public void deleteShop(Integer bid, Integer cid) {
        shopCarMapper.deleteShopByBookIdAndCId(bid, cid);
    }
}
